/************************************************************************/
/* huajiang.liu
/* c++ 大作业
/************************************************************************/
 
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;
static const int NUM = 5; 

static const string SolidNamesList[NUM] = { "dragon","ninja","iceman","lion","wolf" };
static const int SolidCostList[NUM] = {1,2,3,4,5};
static const int SolidAttractList[NUM] = {5,4,3,2,1};
static const string WeaponName[3] = {"sword", "bomb", "arrow"};

class Warrior;
class Weapon;
class Dragon;
class Ninja;
class Lion;
class Iceman;
class HeadQuarter;



class Warrior {
 public:
  Warrior(){};
  Warrior(int id, int life_value, int attact, string pname, string pcolor):id_(id), life_value_(life_value), attact_(attact), pname_(pname), pcolor_(pcolor){};
	void PrintResult(int time, int n, int warrios_num) {
		cout << setw(3) << setfill('0') << time << " " << pcolor_ << " " << pname_ << " "
			     << n << " born with strength " << life_value_ << "," << warrios_num << " "
				 << pname_ <<  " in " << pcolor_ << " headquarter" << endl;
	}
	void Init(int id, int life_value, int attact, string pname, string pcolor) {
		id_ = id;
		life_value_ = life_value;
		attact_ = attact;
		pname_ = pname;
		pcolor_ = pcolor;
	};
  int id_, life_value_, attact_;
  string pname_;
  string pcolor_;
};

class Weapon {
	public:
	Weapon() {weapon_id = 0;};
	public:
	int weapon_id; // 0,1,2分别代表sword, bomb,和arrow
};

class Dragon:public Warrior {
public:
 Dragon(int id, int life_value, int attact, string pname, string pcolor, int total_life_value, int n):Warrior(id, life_value, attact, pname, pcolor) {
	morale_ = total_life_value / life_value_;
	weapon_.weapon_id = n % 3;
 };
//  void GetMorale(int total_life_value) {
// 	morale_ = total_life_value / life_value_; //
//  };
//  void GetWeapon(int n) {
// 	weapon_.weapon_id = n % 3;
//  };
 void PrintResult(int time, int n, int warrios_num) {
	Warrior::PrintResult(time, n, warrios_num);
	cout << "It has a " << WeaponName[weapon_.weapon_id] << " and it's morale is " << morale_ << endl;
 };
private:
	double morale_;
	Weapon weapon_;
};

class Ninja:public Warrior{
public:
	Ninja(int id, int life_value, int attact, string pname, string pcolor, int total_life_value, int n):Warrior(id, life_value, attact, pname, pcolor) {
	weapons_[0].weapon_id = n % 3;
	weapons_[1].weapon_id = (n+1) % 3;
 };
//  void GetWeapon(int n) {
// 	weapons_[0].weapon_id = n % 3;
// 	weapons_[1].weapon_id = (n+1) % 3;
//  }
 void PrintResult(int time, int n, int warrios_num) {
	Warrior::PrintResult(time, n, warrios_num);
	cout << "It has a " << WeaponName[weapons_[0].weapon_id] << "and a " << WeaponName[weapons_[1].weapon_id] << endl;
 }
private:
 Weapon weapons_[2];
};

class Lion:public Warrior{
public:
	Lion(int id, int life_value, int attact, string pname, string pcolor, int total_life_value, int n):Warrior(id, life_value, attact, pname, pcolor) {
	loyalty_ = total_life_value;
 };
//  void GetLoyalty(int total_life_value) {
//   loyalty_ = total_life_value;
//  }
 void PrintResult(int time, int n, int warrios_num) {
	Warrior::PrintResult(time, n, warrios_num);
	cout << "It's loyalty is  " << loyalty_ << endl;
 };
 private:
 int loyalty_;
};

class IceMan:public Warrior {
public:
		IceMan(int id, int life_value, int attact, string pname, string pcolor, int total_life_value, int n):Warrior(id, life_value, attact, pname, pcolor) {
	weapon_.weapon_id = n % 3;
		};
void PrintResult(int time, int n, int warrios_num) {
	Warrior::PrintResult(time, n, warrios_num);
	cout << "It has a " << WeaponName[weapon_.weapon_id] << endl;
 };
private:
	Weapon weapon_;
 };

class HeadQuarter {
 public:
 HeadQuarter(string pcolor, int life_value, int cost_list[], int warrior_list[]):
 		pcolor_(pcolor), life_value_(life_value) {
	 n_ = 0;
	for (int i = 0; i < NUM; i++) {
		warrior_list_[i] = warrior_list[i];
		warrior_life_cost_[i] = cost_list[i];
		warrior_names_[i] = SolidNamesList[i];
		warrior_attacks_[i] = SolidAttractList[warrior_list_[i]];
		warrior_num_list_[i] = 0;
		if (i == 0) {
			min_life_value_ = warrior_life_cost_[i];
		} else {
			min_life_value_ = std::min(min_life_value_, warrior_life_cost_[i]);
		}
	}
	warriors_.clear();
	total_warrior_num_ = 1000;
	make_warrior_success_ = false;
	stop_state_ = false;
	warrior_index_ = 0;
	ptdragon_ = nullptr;
	ptninja_ = nullptr;
	pticeman_ = nullptr;
	ptlion_ = nullptr;
	ptwolf_ = nullptr;
 };

 ~HeadQuarter() {
	 delete ptdragon_;
	 delete ptninja_;
	 delete pticeman_;
	 delete ptlion_;
	 delete ptwolf_;
	 };

 void Makewarrior(int time) {
	if (stop_state_) return;
	if (n_ > total_warrior_num_) {
		cout << setw(3) << setfill('0') << time << " " << pcolor_ << "headquarter stops making warriors" << endl;
		stop_state_ = true;
	}
	
	// cout << time << "->warrior_index->" << warrior_index << "warrior->" << warrior << "warrior_names->" << warrior_names_[warrior] << endl;
	make_warrior_success_ = false;

	while (!make_warrior_success_) {
		int warrior = warrior_list_[warrior_index_ % 5];
		if (warrior_life_cost_[warrior] <= life_value_) {
			// warriors_.emplace_back(warrior(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_));
			switch (warrior)
			{
			case 0:
				++n_;
				life_value_ -= warrior_life_cost_[warrior];
				++warrior_index_;
			    ptdragon_ = new Dragon(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_, life_value_, n_);
				ptdragon_->PrintResult(time, n_, ++warrior_num_list_[warrior]);
				ptdragon_ = nullptr;
				make_warrior_success_ = true;
				break;
			case 1:
			  ++n_;
				life_value_ -= warrior_life_cost_[warrior];
				++warrior_index_;
			    ptninja_ = new Ninja(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_, life_value_, n_);
				ptninja_->PrintResult(time, n_, ++warrior_num_list_[warrior]);
				ptninja_ = nullptr;
				make_warrior_success_ = true;
				break;
			case 2:
			  ++n_;
				life_value_ -= warrior_life_cost_[warrior];
				++warrior_index_;
			    pticeman_ = new IceMan(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_, life_value_, n_);
				pticeman_->PrintResult(time, n_, ++warrior_num_list_[warrior]);
				pticeman_ = nullptr;
				make_warrior_success_ = true;
				break;
			case 3:
			  ++n_;
				life_value_ -= warrior_life_cost_[warrior];
				++warrior_index_;
			    ptlion_ = new Lion(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_, life_value_, n_);
				ptlion_->PrintResult(time, n_, ++warrior_num_list_[warrior]);
				ptlion_ = nullptr;
				make_warrior_success_ = true;
				break;
			case 4:
			  ++n_;
				life_value_ -= warrior_life_cost_[warrior];
				++warrior_index_;
			    ptwolf_ = new Warrior(n_, warrior_life_cost_[warrior], warrior_attacks_[warrior], warrior_names_[warrior], pcolor_);
				ptwolf_->PrintResult(time, n_, ++warrior_num_list_[warrior]);
				ptwolf_ = nullptr;
				make_warrior_success_ = true;
				break;
			default:
				break;
			}
			
		} else {
			++warrior_index_;
		}
		if (life_value_ < min_life_value_ && !make_warrior_success_) {
			stop_state_ = true;
			cout << setw(3) << setfill('0') << time << " " << pcolor_ << " headquarter stops making warriors" << endl;
			break;
		}
	}


 };

 bool StopState() {
	 return stop_state_;
 };
 private:
 int life_value_, n_;
 string pcolor_;
 int warrior_list_[NUM]; // 小兵出生顺序 
 int total_warrior_num_;
 string warrior_names_[NUM];
 int warrior_life_cost_[NUM]; //
 int warrior_attacks_[NUM];
 vector<Warrior> warriors_;
 int warrior_num_list_[NUM];
 bool make_warrior_success_;
 bool stop_state_;
 int warrior_index_;
 int min_life_value_;
 Dragon* ptdragon_;
 Ninja* ptninja_;
 IceMan* pticeman_;
 Lion* ptlion_;
 Warrior* ptwolf_;
};




int main() {
 int cost_list[NUM] = {0,1,2,3,4};
 int case_num = 0;
 int red_warrior_list[NUM] = {2,3,4,1,0}; // 小兵出生顺序 0,1,2,3,4分别代表"dragon","ninja","iceman","lion","wolf"
 int blue_warrior_list[NUM] = {3,0,1,2,4}; // 小兵出生顺序 0,1,2,3,4分别代表"dragon","ninja","iceman","lion","wolf"
 cin >> case_num;
 for (int i =0; i < case_num; i++) {
	 int life_value = 0;
	 cin >> life_value;
	 for (int j = 0;j < NUM; j++) {
		 cin >> cost_list[j];
	 }
	HeadQuarter red_headquarter("red", life_value, cost_list, red_warrior_list);
    HeadQuarter blue_headquarter("blue", life_value, cost_list, blue_warrior_list);
	cout << "case " << i << ":" <<  endl;
	int t = 0;
	while(1) {
		red_headquarter.Makewarrior(t);
		blue_headquarter.Makewarrior(t);
		++t;
		if (red_headquarter.StopState() && blue_headquarter.StopState()) break;
	}
	
 }

}